package com.tanhua.backmanage.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.backmanage.vo.CommentVoQs;
import com.tanhua.backmanage.vo.PageResultQs;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.QuanZiApi;
import com.tanhua.dubbo.server.api.QueryAllCommentsApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {

    @Reference(version = "1.0.0")
    private QueryAllCommentsApi queryAllCommentsApi;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference(version = "1.0.0")
    private QuanZiApi quanZiApi;
//    private UserInfoService userInfoService;

//    private QueryAlluserInfosApi queryAlluserInfosApi;
    /**
     * 查询评论列表
     *
     * @param page
     * @param pageSize
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param publishId
     * @return
     */
    public PageResultQs queryAllCommentsList(Integer page, Integer pageSize, String sortProp, String sortOrder, String publishId) {

        PageResultQs pageResultQs = new PageResultQs();
        pageResultQs.setPage(page);
        pageResultQs.setPagesize(pageSize);

        Long pageCount = quanZiApi.queryCommentCount(publishId);
        Integer ceil =  ((int) Math.ceil(pageCount / pageSize)+1);
        pageResultQs.setCounts(Convert.toInt(pageCount));
        pageResultQs.setPages(ceil);


        PageInfo<Comment> pageInfo = queryAllCommentsApi.queryCommentList(page, pageSize, sortProp, sortOrder, publishId);
        List<Comment> records = pageInfo.getRecords();

        //利用找出来的评论将评论的用户id找出来
        List<Object> userIds = CollUtil.getFieldValues(records, "userId");
        List<UserInfo> userInfos = queryUserInfoByUserIdListQs(userIds);

        //填充信息
        List<CommentVoQs> commentVoQss = new ArrayList<>();
        for (Comment record : records) {
            CommentVoQs commentVoQs = new CommentVoQs();
            commentVoQs.setId(record.getId().toHexString());
            commentVoQs.setUserId(Convert.toInt(record.getUserId()));
            commentVoQs.setContent(record.getContent());
//            commentVoQs.setCreateDate(DateUtil.format(new Date(record.getCreated()), "HH:mm"));
//            commentVoQs.setCreateDate(Convert.toLong(new Date()));
            commentVoQs.setCreateDate(record.getCreated());

            for (UserInfo userInfo : userInfos) {
                if (ObjectUtil.equals(record.getUserId(), userInfo.getUserId())) {
                    commentVoQs.setNickname(userInfo.getNickName());
                    break;
                }
            }
            commentVoQss.add(commentVoQs);
        }
        pageResultQs.setItems(commentVoQss);
        return pageResultQs;


    }

    /**
     * 根据userId查询userInfo
     *
     * @param userIdList
     * @return
     */
    public List<UserInfo> queryUserInfoByUserIdListQs(List<Object> userIdList){
        QueryWrapper<UserInfo> queryWrapper= new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        return userInfos;
    }

}
